package com.yby.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yby.common.utils.PageUtils;
import com.yby.common.utils.R;
import com.yby.mall.product.dao.BrandDao;
import com.yby.mall.product.dao.CategoryDao;
import com.yby.mall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yby.common.utils.Query;

import com.yby.mall.product.dao.CategoryBrandRelationDao;
import com.yby.mall.product.entity.CategoryBrandRelationEntity;
import com.yby.mall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;

    @Override
    public R queryPage(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationList = categoryBrandRelationDao.selectList(Wrappers.<CategoryBrandRelationEntity>lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, brandId));
        return R.ok().put("data",categoryBrandRelationList);
    }

    @Override
    @Transactional
    public R saveCategoryBrandRelation(CategoryBrandRelationVo categoryBrandRelationVo) {
        if(categoryBrandRelationVo.getCatelogId() == null){
            return R.error("请选择关联分类");
        }
        Integer count = categoryBrandRelationDao.selectCount(Wrappers.<CategoryBrandRelationEntity>lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, categoryBrandRelationVo.getBrandId())
                .eq(CategoryBrandRelationEntity::getCatelogId, categoryBrandRelationVo.getCatelogId()));
        if(count > 0){
            return R.error("已存在该分类");
        }else{
            CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
            BeanUtils.copyProperties(categoryBrandRelationVo,entity);
            String brandName = brandDao.selectById(categoryBrandRelationVo.getBrandId()).getName();
            String categoryName = categoryDao.selectById(categoryBrandRelationVo.getCatelogId()).getName();
            entity.setBrandName(brandName);
            entity.setCatelogName(categoryName);
            categoryBrandRelationDao.insert(entity);
        }
        return R.ok("保存成功");
    }

    @Override
    public R brandsList(Long catId) {
        List<CategoryBrandRelationEntity> list = categoryBrandRelationDao.selectList(Wrappers.<CategoryBrandRelationEntity>lambdaQuery().eq(CategoryBrandRelationEntity::getCatelogId, catId));
        return R.ok().put("data",list);
    }
}